#include <QList>
#include "DataManager.h"

static const QString test[9][9] = {
    {"7",  "", "9",  "",  "", "2", "3",  "",  ""},
    {"4",  "", "6", "3", "5",  "", "9",  "",  ""},
    {"2",  "",  "", "9",  "", "4",  "", "1",  ""},
    { "",  "",  "", "1",  "", "9",  "",  "",  ""},
    { "", "3", "1", "4",  "", "5", "2", "7",  ""},
    { "",  "",  "", "7",  "", "8",  "",  "",  ""},
    { "", "2",  "", "8",  "", "6",  "",  "", "9"},
    { "",  "", "8",  "", "4", "7", "6",  "", "5"},
    { "",  "", "4", "5",  "",  "", "1",  "", "2"}
};

DataManager* DataManager::m_inst = NULL;

DataManager::DataManager()
{
}

DataManager::~DataManager()
{

}

DataManager* DataManager::inst()
{
    if (NULL == m_inst) {
        m_inst = new DataManager();
    }
    return m_inst;
}

QString DataManager::getString(int x, int y)
{
    return test[x][y];
}

bool DataManager::checkRow(int rowIndex, const myPoint &resPoint, QList<myPoint> &sameList)
{
    sameList.clear();
    bool ret = true;
    int x = resPoint.x();
    int y = resPoint.y();
    int g = resPoint.group();
    QString strTmp = test[x + (g/3)*3][y + (g%3)*3];
    for (int i = 0; i < 9; ++i) {
        if ((test[rowIndex][i] == strTmp)
                && (i != (y + (g%3)*3))) {
            int xTmp = rowIndex%3;
            int yTmp = i%3;
            int grpTmp = (rowIndex%3)*3 + i/3;
            sameList.append(myPoint(xTmp, yTmp, grpTmp));
            ret = false;
        }
    }
    return ret;
}

bool DataManager::checkCol(int colIndex, const myPoint &resPoint, QList<myPoint> &sameList)
{
    sameList.clear();
    bool ret = true;
    int x = resPoint.x();
    int y = resPoint.y();
    int g = resPoint.group();
    QString strTmp = test[x + (g/3)*3][y + (g%3)*3];
    for (int i = 0; i < 9; ++i) {
        if ((test[i][colIndex] == strTmp)
                && (i != (y + (g%3)*3))) {
            int xTmp = i%3;
            int yTmp = colIndex%3;
            int grpTmp = (i%3)*3 + colIndex/3;
            sameList.append(myPoint(xTmp, yTmp, grpTmp));
            ret = false;
        }
    }
    return ret;
}

bool DataManager::checkGroup(const myPoint &resPoint, QList<myPoint> &sameList)
{
    sameList.clear();
    bool ret = true;
    int x = resPoint.x();
    int y = resPoint.y();
    int g = resPoint.group();
    QString strTmp = test[x + (g/3)*3][y + (g%3)*3];
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            if ((test[i + (g/3)*3][j + (g%3)*3] == strTmp)
                    && (i != x)
                    && (j != y)) {
                int xTmp = i;
                int yTmp = j;
                int grpTmp = g;
                sameList.append(myPoint(xTmp, yTmp, grpTmp));
                ret = false;
            }
        }
    }
    return ret;
}

bool DataManager::autoFillBlanks()
{
    bool ret = true;

    return ret;
}
